<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>天气查询</title>
    <style>
        .container { max-width: 600px; margin: 50px auto; padding: 20px; }
        input { padding: 10px; width: 200px; margin-right: 10px; }
        button { padding: 10px 20px; background: #007bff; color: white; border: none; cursor: pointer; }
        #result { margin-top: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 4px; }
        .error { color: #dc3545; background: #f8d7da; border-color: #f5c6cb; }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="city" placeholder="输入城市名称，如：北京">
        <button onclick="getWeather()">查询天气</button>
        <div id="result"></div>
    </div>

    <script>
        // 请先注册和风天气获取API_KEY: https://dev.qweather.com/
        const API_KEY = "sk-mfStYD0AJWRGK3Rm653d8f05160a48F282F59a153677578a";
        
        async function getWeather() {
            const cityInput = document.getElementById("city");
            const resultDiv = document.getElementById("result");
            resultDiv.className = ""; // 清除错误样式
            resultDiv.innerHTML = "查询中...";

            const city = cityInput.value.trim();
            if (!city) {
                showError("请输入有效的城市名称");
                return;
            }

            try {
                // 步骤1: 先获取城市地理编码
                const locationRes = await fetch(
                    `https://geoapi.qweather.com/v2/city/lookup?location=${encodeURIComponent(city)}&key=${API_KEY}`
                );
                
                if (!locationRes.ok) throw new Error(`地理编码请求失败: ${locationRes.status}`);
                
                const locationData = await locationRes.json();
                if (locationData.code !== "200" || !locationData.location?.length) {
                    throw new Error("找不到该城市，请检查名称是否正确");
                }

                // 获取城市ID
                const locationId = locationData.location[0].id;
                
                // 步骤2: 获取实时天气数据
                const weatherRes = await fetch(
                    `https://api.qweather.com/v7/weather/now?location=${locationId}&key=${API_KEY}`
                );
                
                if (!weatherRes.ok) throw new Error(`天气请求失败: ${weatherRes.status}`);
                
                const weatherData = await weatherRes.json();
                if (weatherData.code !== "200") {
                    throw new Error(weatherData.message || "天气数据获取失败");
                }

                // 显示结果
                resultDiv.innerHTML = `
                    <h3>${locationData.location[0].name}实时天气</h3>
                    <p>温度：${weatherData.now.temp}°C</p>
                    <p>体感温度：${weatherData.now.feelsLike}°C</p>
                    <p>天气状况：${weatherData.now.text}</p>
                    <p>湿度：${weatherData.now.humidity}%</p>
                    <p>风向：${weatherData.now.windDir} ${weatherData.now.windScale}级</p>
                `;

            } catch (error) {
                console.error(error);
                showError(error.message);
            }
        }

        function showError(msg) {
            const resultDiv = document.getElementById("result");
            resultDiv.className = "error";
            resultDiv.innerHTML = `错误: ${msg}`;
        }
    </script>
</body>
</html>